package com.wys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wys.dao.OrderDao;
import com.wys.dto.OrdersDto;
import com.wys.pojo.Orders;
import com.wys.pojo.ShoppingCart;
import com.wys.result.R;
import com.wys.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     *  用户下单
     * @param orders
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders,HttpSession session){
        orderService.submit(orders,session);
        return R.success("下单成功！");
    }

    /**
     *  移动端查看订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> getOrders(int page, int pageSize,HttpSession session){
        return R.success(orderService.getOrders(page,pageSize,session));
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> getAgain(@RequestBody Orders orders, HttpSession session){
        return orderService.getAgain(session,orders);
    }

    /**
     *  管理端订单查询
     * @param page
     * @param pageSize
     * @param number 订单编号
     * @param beginTime  开始日期
     * @param endTime    截至日期
     * @return
     */
    @GetMapping("/page")
    public R<Page> getUserPage(int page, int pageSize, String number, String beginTime,String endTime){
        Page userPage = orderService.getUserPage(page, pageSize, number, beginTime, endTime);
        if(page > userPage.getPages()){
            userPage = orderService.getUserPage((int) userPage.getPages(),pageSize,number,beginTime,endTime);
        }
        return R.success(userPage);
    }

    /**
     * 更新订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("状态修改成功！");
    }

}
